package com.kaochong.xcx.web.controller;

import com.kaochong.xcx.common.constant.CommonConstant;
import com.kaochong.xcx.service.ShareService;
import com.kaochong.xcx.utils.response.ResponseUtils;
import com.kaochong.xcx.utils.response.ResultVO;
import com.kaochong.xcx.web.aspect.CurrentUser;
import com.kaochong.xcx.web.controller.base.BasicController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

/**
 * @author han
 * @date 2018-06-25 11:38
 */
@RestController
@RequestMapping("/recite/friend")
@Slf4j
public class ShareController extends BasicController {
	
	@Autowired
	ShareService shareService;
	
	@GetMapping("/help")
	public ResultVO help(@RequestParam(value = "openId") String openId,
	                     @RequestParam(value = "target") Integer target,
	                     @RequestParam(value = "stage") Integer stage) {
		CurrentUser user = takeUser();
		shareService.friendHelp(user, openId, target, stage);
		return ResponseUtils.success();
	}
	
	@GetMapping("/help/list")
	public ResultVO helpList(Map<String, Object> map) {
		CurrentUser user = takeUser();
		Integer target = user.getTarget() == null ? 0: user.getTarget();
		Integer stage = user.getCurrStage();
		List<Map<String, Object>> result = shareService.friendHelpList(user.getOpenId(), target, stage);
		if (result.size() >= CommonConstant.FRIEND_HELP_COUNT) {
			map.put("status", 1);
		} else {
			map.put("status", 0);
		}
		map.put("friends", result);
		return ResponseUtils.success(map);
	}
	
	@GetMapping("/help/status")
	public ResultVO helpCount(Map<String, Object> map) {
		CurrentUser user = takeUser();
		Integer target = user.getTarget() == null ? 0: user.getTarget();
		Integer stage = user.getCurrStage();
		Integer count = shareService.friendHelpCount(user.getOpenId(), target, stage);
		if (count >= CommonConstant.FRIEND_HELP_COUNT) {
			map.put("status", 1);
		} else {
			map.put("status", 0);
		}
		return ResponseUtils.success(map);
	}
}
